package cn.tedu.boot21.controller;

import cn.tedu.boot21.entity.Emp;
import cn.tedu.boot21.utils.DBUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.*;
import java.util.ArrayList;

@Controller
public class EmpController {

    @RequestMapping("/add")
    public void add(Emp emp,HttpServletResponse response) throws IOException {
        System.out.println("emp = " + emp);
        //获取连接
        try (Connection conn = DBUtils.getConn()){
            String sql = "insert into myemp values(null,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setString(1,emp.getName());
            ps.setInt(2,emp.getSal());
            ps.setString(3,emp.getJob());
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        response.sendRedirect("/select");
    }

    @RequestMapping("/select")
    @ResponseBody
    public String select(){
        //创建一个集合用来装所有的员工对象
        ArrayList<Emp> list = new ArrayList<>();
        //获取连接
        try (Connection conn = DBUtils.getConn()){
            String sql = "select * from myemp";
            Statement s = conn.createStatement();//因为没有变量所以用这个
            ResultSet rs = s.executeQuery(sql);//执行查询
            //遍历结果集
            while(rs.next()){
                int id = rs.getInt(1);
                String name= rs.getString(2);
                int sal = rs.getInt(3);
                String job = rs.getString(4);
                //把上面4个参数封装到Emp对象中 并把emp对象装进list集合
                Emp e = new Emp(id,name,sal,job);
                list.add(e);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //此时得到了从数据库查询到的所有员工信息,
        String html = "<table border=1>";
        html+="<caption>员工列表</caption>";
        html+="<tr><th>id</th><th>名字</th><th>工资</th><th>工作</th><th>操作</th></tr>";
        //遍历集合 往table标签中添加一行行的数据
        for (Emp e : list) {
            html+="<tr>";
            html+="<td>"+e.getId()+"</td>";
            html+="<td>"+e.getName()+"</td>";
            html+="<td>"+e.getSal()+"</td>";
            html+="<td>"+e.getJob()+"</td>";
            //在请求地址的后加上?,在?后面写传递的参数 把id传递过去
            html+="<td><a href='/delete?id="+e.getId()+"'>删除</a></td>";

            html+="</tr>";
        }
        html+="</table>";
        return html;//把拼接好的html标签返回给客户端浏览器去展示
    }

    @RequestMapping("/delete")
    public void delete(int id, HttpServletResponse response) throws IOException {
        System.out.println("id = " + id);
        //获取连接
        try (Connection conn = DBUtils.getConn()){
            String sql = "delete from myemp where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
            ps.setInt(1,id);
            ps.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //重定向到列表页面 异常抛出
        response.sendRedirect("/select");
    }
    @RequestMapping("/update")
    public void update(Emp emp, HttpServletResponse response) throws IOException {
        //获取连接
        try (Connection conn = DBUtils.getConn()){
            String sql = "update myemp set name=?,sal=?,job=?where id=?";
            PreparedStatement ps = conn.prepareStatement(sql);
           ps.setString(1,emp.getName());
           ps.setInt(2,emp.getSal());
           ps.setString(3,emp.getJob());
           ps.setInt(4,emp.getId());
            ps.executeUpdate();//执行修改
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        //重定向到列表页面 异常抛出
        response.sendRedirect("/select");
    }
}
